///////////////////////////////////////////////////////////////////////////////
// Demo Workshop Viewer Extension
///////////////////////////////////////////////////////////////////////////////
BimsopNamespace("Bimsop.Extension");

Bimsop.Extension.Workshop = function(viewer, options) {
  /////////////////////////////////////////////////////////////////
  //  base class constructor
  //
  /////////////////////////////////////////////////////////////////
  Bimsop.Viewing.Extension.call(this, viewer, options);

  var _self = this;

  var _panel = null;

  var _interval = null;

  /////////////////////////////////////////////////////////////////
  // load callback: invoked when viewer.loadExtension is called
  //
  /////////////////////////////////////////////////////////////////
  _self.load = function() {
    viewer.addEventListener(
      Bimsop.Viewing.SELECTION_CHANGED_EVENT,
      onSelectionChanged
    );

    _panel = new Bimsop.WorkshopPanel(
      viewer.container,
      "WorkshopPanelId",
      "Workshop Panel"
    );

    _interval = 0;

    console.log("Bimsop.Extension.Workshop loaded");

    return true;
  };

  /////////////////////////////////////////////////////////////////
  // unload callback: invoked when viewer.unloadExtension is called
  //
  /////////////////////////////////////////////////////////////////
  _self.unload = function() {
    viewer.removeEventListener(
      Bimsop.Viewing.SELECTION_CHANGED_EVENT,
      onSelectionChanged
    );

    _panel.setVisible(false);

    _panel.uninitialize();

    clearInterval(_interval);

    console.log("Bimsop.Extension.Workshop unloaded");

    return true;
  };

  /////////////////////////////////////////////////////////////////
  // selection changed callback
  //
  /////////////////////////////////////////////////////////////////
  function onSelectionChanged(event) {
    function propertiesHandler(result) {
      if (result.parameters) {
        console.log(result.parameters);
        // gPanel = _panel;

        // _panel.setProperties(
        //   result.parameters);

        // _panel.setVisible(true);
      }
    }

    if (event.dbIdArray.length) {
      var dbId = event.dbIdArray[0];

      console.log(dbId);

      viewer.models[0].getProperties(dbId, propertiesHandler);

      viewer.fitToView(dbId);

      viewer.isolate(dbId);

      startRotation();
    } else {
      clearInterval(_interval);

      viewer.isolate([]);

      viewer.fitToView();

      // _panel.setVisible(false);
    }
  }

  /////////////////////////////////////////////////////////////////
  // rotates camera around axis with center origin
  //
  /////////////////////////////////////////////////////////////////
  function rotateCamera(angle, axis) {
    var pos = viewer.navigation.getPosition();

    var position = new THREE.Vector3(pos.x, pos.y, pos.z);

    var rAxis = new THREE.Vector3(axis.x, axis.y, axis.z);

    var matrix = new THREE.Matrix4().makeRotationAxis(rAxis, angle);

    position.applyMatrix4(matrix);

    viewer.navigation.setPosition(position);
  }

  /////////////////////////////////////////////////////////////////
  // start rotation effect
  //
  /////////////////////////////////////////////////////////////////
  function startRotation() {
    clearInterval(_interval);

    setTimeout(function() {
      _interval = setInterval(function() {
        rotateCamera(0.05, { x: 0, y: 1, z: 0 });
      }, 100);
    }, 500);
  }

  /////////////////////////////////////////////////////////////////
  // creates panel and sets up inheritance
  //
  /////////////////////////////////////////////////////////////////
  Bimsop.WorkshopPanel = function(parentContainer, id, title, options) {
    Bimsop.Viewing.UI.PropertyPanel.call(this, parentContainer, id, title);
  };

  Bimsop.WorkshopPanel.prototype = Object.create(
    Bimsop.Viewing.UI.PropertyPanel.prototype
  );

  Bimsop.WorkshopPanel.prototype.constructor = Bimsop.WorkshopPanel;
};

/////////////////////////////////////////////////////////////////
// sets up inheritance for extension and register
//
/////////////////////////////////////////////////////////////////
Bimsop.Extension.Workshop.prototype = Object.create(
  Bimsop.Viewing.Extension.prototype
);

Bimsop.Extension.Workshop.prototype.constructor = Bimsop.Extension.Workshop;

Bimsop.Viewing.theExtensionManager.registerExtension(
  "Bimsop.Extension.Workshop",
  Bimsop.Extension.Workshop
);
